//Copyright 2010 Balazs Keri, Imre Keri
//
//This file is part of FireworksCreator.
//
//FireworksCreator is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.
//
//FireworksCreator is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.
//
//You should have received a copy of the GNU General Public License
//along with FireworksCreator.  If not, see <http://www.gnu.org/licenses/>.
//
#include <QObject>
#include "attrevent.h"
#include "viewattributegroup.h"
#include "document.h"
#include "eventforwarder.h"
#include "view.h"
//
EventForwarder::EventForwarder(View *_target,QObject *parent)
:
   QObject(parent),target(_target)
{
}
EventForwarder::~EventForwarder()
{
}
void EventForwarder::recv_attr_event(AttrEvent *evnt)
{
   if(evnt->get_origin()!=target)
   {
      target->handle_attr_event(evnt);
   }
}
void EventForwarder::recv_pre_primitive(const Primitive &prim)
{
   target->handle_pre_primitive(prim);
}
void EventForwarder::recv_post_primitive(const Primitive &prim)
{
   target->handle_post_primitive(prim);
}
void EventForwarder::recv_fail_primitive(const Primitive &prim)
{
   target->handle_fail_primitive(prim);
}
void EventForwarder::recv_post_primitive_group(const PrimitiveGroup &prim_grp)
{
   target->handle_post_primitive_group(prim_grp);
}
//
View::View()
:
   eventForwarder(new EventForwarder(this))
{
}
View::~View()
{
}
void View::set_attr_group(const QSharedPointer<ViewAttributeGroup> &group)
{
   if(group==attr_group)
      return;
   if(attr_group)
   {
      attr_group->disconnect(eventForwarder.data(),SLOT(recv_attr_event(AttrEvent*)));
      if(attr_group->get_document())
      {
         attr_group->get_document()->disconnect(eventForwarder.data(),SLOT(recv_pre_primitive(const Primitive&)));
         attr_group->get_document()->disconnect(eventForwarder.data(),SLOT(recv_post_primitive(const Primitive&)));
         attr_group->get_document()->disconnect(eventForwarder.data(),SLOT(recv_fail_primitive(const Primitive&)));
         attr_group->get_document()->disconnect(eventForwarder.data(),SLOT(recv_post_primitive_group(const PrimitiveGroup&)));
      }
   }
   attr_group=group;
   if(attr_group)
   {
      eventForwarder->connect(attr_group.data(),SIGNAL(data_changed(AttrEvent*)),SLOT(recv_attr_event(AttrEvent*)));
      if(attr_group->get_document())
      {
         eventForwarder->connect(attr_group->get_document(),SIGNAL(pre_primitive(const Primitive&)),SLOT(recv_pre_primitive(const Primitive&)));
         eventForwarder->connect(attr_group->get_document(),SIGNAL(post_primitive(const Primitive&)),SLOT(recv_post_primitive(const Primitive&)));
         eventForwarder->connect(attr_group->get_document(),SIGNAL(fail_primitive(const Primitive&)),SLOT(recv_fail_primitive(const Primitive&)));
         eventForwarder->connect(attr_group->get_document(),SIGNAL(post_primitive_group(const PrimitiveGroup&)),SLOT(recv_post_primitive_group(const PrimitiveGroup&)));
      }
   }
   handle_set_attr_group();
}
void View::handle_attr_event(AttrEvent* /*event*/)
{
   //default: do nothing
}
/*void View::handle_set_document() {
   //default: do nothing
}*/
void View::handle_set_attr_group()
{
   //default: do nothing
}
void View::handle_pre_primitive(const Primitive &/*prim*/)
{
}
void View::handle_post_primitive(const Primitive &/*prim*/)
{
}
void View::handle_fail_primitive(const Primitive &/*prim*/)
{
}
void View::handle_post_primitive_group(const PrimitiveGroup &/*prim_grp*/)
{
}
